var core = require('core');
var isUser = require('./isUser');
var reg = require('./reg');
var location = function(data, callback){
    locations = function(){
        core.api('user/location', data, function(err, doc){
            try{
                if(doc.code)
                    throw new Error(doc.msg);
                if(callback && typeof callback === 'function') callback(doc);
            }catch(e){
                console.error(e.message);
                if(callback && typeof callback === 'function') callback(e);
            }

        });
    };

    isUser({openid: data.openid}, function(err, docs){
        if(!docs || !docs.docs)
            reg({openid: data.openid},locations);
        else locations();
    });

};

module.exports = location;
